import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../models/song.dart';
import '../models/music_source.dart';

class SongListWidget extends StatelessWidget {
  final List<Song> songs;
  final Function(Song, int) onSongTap;
  final bool showRanking;
  final Function(Song, int)? onDeleteTap; // 添加删除回调

  const SongListWidget({
    super.key,
    required this.songs,
    required this.onSongTap,
    this.showRanking = false,
    this.onDeleteTap, // 可选的删除回调
  });

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: songs.length,
      // 减少列表项之间的间距
      padding: const EdgeInsets.symmetric(vertical: 2),
      itemBuilder: (context, index) {
        final song = songs[index];
        return SongListTile(
          song: song,
          onTap: () => onSongTap(song, index),
          showRanking: showRanking,
          ranking: index + 1,
          onDeleteTap: onDeleteTap != null ? () => onDeleteTap!(song, index) : null,
        );
      },
    );
  }
}

class SongListTile extends StatelessWidget {
  final Song song;
  final VoidCallback onTap;
  final bool showRanking;
  final int ranking;
  final VoidCallback? onDeleteTap; // 添加删除回调

  const SongListTile({
    super.key,
    required this.song,
    required this.onTap,
    this.showRanking = false,
    this.ranking = 0,
    this.onDeleteTap, // 可选的删除回调
  });

  @override
  Widget build(BuildContext context) {
    return ListTile(
      // 减少ListTile的内边距
      contentPadding: const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
      // 减少ListTile的最小高度
      minVerticalPadding: 0,
      dense: true,
      visualDensity: const VisualDensity(horizontal: -1, vertical: -2),
      leading: showRanking 
          ? Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(
                  width: 24,
                  child: Text(
                    ranking.toString(),
                    style: TextStyle(
                      fontSize: 12,
                      fontWeight: FontWeight.w500,
                      color: ranking <= 3 
                          ? Theme.of(context).colorScheme.primary
                          : Theme.of(context).colorScheme.onSurfaceVariant,
                    ),
                    textAlign: TextAlign.center,
                  ),
                ),
                const SizedBox(width: 4),
                ClipRRect(
                  borderRadius: BorderRadius.circular(4),
                  child: SizedBox(
                    width: 32,
                    height: 32,
                    child: song.albumImageUrl != null
                        ? CachedNetworkImage(
                            imageUrl: song.albumImageUrl!,
                            fit: BoxFit.cover,
                            placeholder: (context, url) => Container(
                              color: Theme.of(context).colorScheme.surfaceVariant,
                              child: const Icon(Icons.music_note, size: 16),
                            ),
                            errorWidget: (context, url, error) => Container(
                              color: Theme.of(context).colorScheme.surfaceVariant,
                              child: const Icon(Icons.music_note, size: 16),
                            ),
                          )
                        : Container(
                            color: Theme.of(context).colorScheme.surfaceVariant,
                            child: const Icon(Icons.music_note, size: 16),
                          ),
                  ),
                ),
              ],
            )
          : ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: SizedBox(
                width: 36,
                height: 36,
                child: song.albumImageUrl != null
                    ? CachedNetworkImage(
                        imageUrl: song.albumImageUrl!,
                        fit: BoxFit.cover,
                        placeholder: (context, url) => Container(
                          color: Theme.of(context).colorScheme.surfaceVariant,
                          child: const Icon(Icons.music_note, size: 18),
                        ),
                        errorWidget: (context, url, error) => Container(
                          color: Theme.of(context).colorScheme.surfaceVariant,
                          child: const Icon(Icons.music_note, size: 18),
                        ),
                      )
                    : Container(
                        color: Theme.of(context).colorScheme.surfaceVariant,
                        child: const Icon(Icons.music_note, size: 18),
                      ),
              ),
            ),
      title: Text(
        song.name,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: 13,
          fontWeight: FontWeight.normal,
          color: Theme.of(context).colorScheme.onSurface,
        ),
      ),
      subtitle: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            song.artistsString,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              fontSize: 11,
              fontWeight: FontWeight.normal,
              color: Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
            ),
          ),
          if (song.album.isNotEmpty)
            Text(
              song.album,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 10,
                fontWeight: FontWeight.normal,
                color: Theme.of(context).colorScheme.onSurface.withOpacity(0.5),
              ),
            ),
        ],
      ),
      trailing: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 音乐源标识
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 3, vertical: 1),
            decoration: BoxDecoration(
              color: Theme.of(context).colorScheme.primary.withOpacity(0.08),
              borderRadius: BorderRadius.circular(3),
            ),
            child: Text(
              MusicSource.fromString(song.source).displayName,
              style: TextStyle(
                fontSize: 9,
                fontWeight: FontWeight.normal,
                color: Theme.of(context).colorScheme.primary,
              ),
            ),
          ),
          const SizedBox(width: 6),
          // 删除按钮（如果提供了删除回调）
          if (onDeleteTap != null) ...[
            IconButton(
              icon: Icon(
                Icons.delete_outline,
                size: 16,
                color: Theme.of(context).colorScheme.error,
              ),
              onPressed: onDeleteTap,
              padding: const EdgeInsets.all(2),
              constraints: const BoxConstraints(
                minWidth: 28,
                minHeight: 28,
              ),
            ),
            const SizedBox(width: 4),
          ],
          Icon(
            Icons.play_arrow,
            size: 18,
            color: Theme.of(context).colorScheme.primary,
          ),
        ],
      ),
      onTap: onTap,
    );
  }
}